﻿<Project Sdk="Microsoft.Build.NoTargets">
  <PropertyGroup>
    <SwapNativeForIL Condition="'$(SwapNativeForIL)' == '' and ('$(Configuration)' == 'Debug' or '$(Coverage)' == 'true') and '$(RuntimeFlavor)' != 'Mono'">true</SwapNativeForIL>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <!-- Binplace properties -->
    <BinPlaceForTargetVertical>false</BinPlaceForTargetVertical>
    <BinPlaceNative>true</BinPlaceNative>
    <BinPlaceRuntime>false</BinPlaceRuntime>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <PropertyGroup>
    <GCStressDependsOnCoreDisTools>false</GCStressDependsOnCoreDisTools>
    <GCStressDependsOnCoreDisTools Condition="'$(TargetOS)' == 'windows' And ('$(TargetArchitecture)' == 'x64' Or '$(TargetArchitecture)' == 'x86')">true</GCStressDependsOnCoreDisTools>
    <GCStressDependsOnCoreDisTools Condition="'$(TargetOS)' == 'linux' And '$(TargetArchitecture)' == 'x64'">true</GCStressDependsOnCoreDisTools>
    <CopyCoreDisToolsToCoreRoot>false</CopyCoreDisToolsToCoreRoot>
    <CopyCoreDisToolsToCoreRoot Condition="$(GCStressDependsOnCoreDisTools) And '$(DotNetBuildFromSource)' != 'true'">true</CopyCoreDisToolsToCoreRoot>
  </PropertyGroup>

  <Import Project="$(RepositoryEngineeringDir)coredistools.targets" Condition="$(CopyCoreDisToolsToCoreRoot)" />

  <ItemGroup Condition="'$(DotNetBuildFromSource)' != 'true'">
    <PackageReference Include="Microsoft.DiaSymReader.Native"
                      Version="$(MicrosoftDiaSymReaderNativeVersion)" />
  </ItemGroup>

  <ItemGroup>
    <HostFxrFile Include="$(DotNetHostBinDir)$(LibPrefix)hostfxr$(LibSuffix)" />
    <HostPolicyFile Include="$(DotNetHostBinDir)$(LibPrefix)hostpolicy$(LibSuffix)" />
    <DotnetExe Include="$(DotNetHostBinDir)dotnet$(ExeSuffix)" />
  </ItemGroup>


  <!-- Setup the testing shared framework host -->
  <Target Name="SetupTestingHost"
          AfterTargets="AfterResolveReferences"
          Condition="Exists('$(DotNetHostBinDir)') and '$(TestNativeAot)' != 'true'">
    <PropertyGroup>
      <UseHardlink>true</UseHardlink>
    </PropertyGroup>

    <!-- We do not need apphost.exe.
         Exclude here so that when building with the 2.x SDK we don't place it in the test shared framework. -->
    <ItemGroup>
      <ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="'%(Filename)' == 'apphost'" />
    </ItemGroup>

    <Copy SourceFiles="@(HostFxrFile)"
          DestinationFolder="$(NetCoreAppCurrentTestHostPath)host\fxr\$(ProductVersion)"
          SkipUnchangedFiles="true"
          UseHardlinksIfPossible="$(UseHardlink)" />

    <Copy SourceFiles="@(HostPolicyFile)"
          DestinationFolder="$(NetCoreAppCurrentTestHostPath)shared\Microsoft.NETCore.App\$(ProductVersion)"
          SkipUnchangedFiles="true"
          UseHardlinksIfPossible="$(UseHardlink)" />

    <Copy SourceFiles="@(DotnetExe)"
          DestinationFolder="$(NetCoreAppCurrentTestHostPath)"
          SkipUnchangedFiles="true"
          UseHardlinksIfPossible="$(UseHardlink)" />

    <!-- Include any native sanitizer runtimes so we can run our tests on machines without the exact same development environment. -->
    <Copy SourceFiles="@(SanitizerRuntimeToCopy->'$(DotNetHostBinDir)%(Identity)')"
          DestinationFolder="$(NetCoreAppCurrentTestHostPath)"
          SkipUnchangedFiles="true"
          UseHardlinksIfPossible="$(UseHardlink)" />

    <Copy Condition="$(CopyCoreDisToolsToCoreRoot)"
          SourceFiles="$(CoreDisToolsLibrary)"
          DestinationFolder="$(NetCoreAppCurrentTestHostPath)shared\Microsoft.NETCore.App\$(ProductVersion)"
          SkipUnchangedFiles="true"
          UseHardlinksIfPossible="$(UseHardlink)" />

    <Exec Command="chmod +x $(NetCoreAppCurrentTestHostPath)%(DotnetExe.Filename)%(DotnetExe.Extension)" Condition="'$(TargetOS)' != 'windows' and '$(OS)' != 'Windows_NT'"/>
  </Target>

  <Target Name="OverrideRuntimeCoreCLR"
          DependsOnTargets="ResolveRuntimeFilesFromLocalBuild"
          AfterTargets="AfterResolveReferences"
          Condition="'$(RuntimeFlavor)' != 'Mono' and '$(UseNativeAotCoreLib)' != 'true'">
    <ItemGroup>
      <RuntimeFiles Include="@(HostFxrFile)" Condition="Exists('@(HostFxrFile)')" />
      <RuntimeFiles Include="@(HostPolicyFile)" Condition="Exists('@(HostPolicyFile)')" />
      <!-- CoreRun is not used for testing anymore, but we still use it for benchmarking and profiling -->
      <RuntimeFiles Include="$(CoreCLRArtifactsPath)\corerun*" />
      <RuntimeFiles Include="$(CoreCLRArtifactsPath)\PDB\corerun*" />
    </ItemGroup>
    <!-- If the build has native sanitizers, copy over the non-sanitized diagnostic binaries so they can be loaded by a debugger -->
    <ItemGroup Condition="'$(EnableNativeSanitizers)' != ''">
      <RuntimeFiles Remove="$(CoreCLRArtifactsPath)\sharedFramework\*mscordaccore*" />
      <RuntimeFiles Remove="$(CoreCLRArtifactsPath)\sharedFramework\*mscordbi*" />
      <RuntimeFiles Remove="$(CoreCLRArtifactsPath)\sharedFramework\PDB\*mscordaccore*" />
      <RuntimeFiles Remove="$(CoreCLRArtifactsPath)\sharedFramework\PDB\*mscordbi*" />
      <RuntimeFiles Include="$(CoreCLRArtifactsPath)\unsanitized\sharedFramework\*" IsNative="true" />
      <RuntimeFiles Include="$(CoreCLRArtifactsPath)\unsanitized\PDB\*mscordaccore*" IsNative="true" />
      <RuntimeFiles Include="$(CoreCLRArtifactsPath)\unsanitized\PDB\*mscordbi*" IsNative="true" />
    </ItemGroup>
    <ItemGroup>
      <ReferenceCopyLocalPaths Include="@(RuntimeFiles)" />
    </ItemGroup>
    <ItemGroup Condition="'$(SwapNativeForIL)' == 'true'">
      <CoreCLRILFiles Include="$(CoreCLRArtifactsPath)\IL\*.*" />
      <ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="'@(CoreCLRILFiles->'%(FileName)%(Extension)')' == '%(FileName)%(Extension)'" />
      <ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="'@(CoreCLRILFiles->'%(FileName).ni%(Extension)')' == '%(FileName)%(Extension)'" />
      <ReferenceCopyLocalPaths Include="@(CoreCLRILFiles)" />
    </ItemGroup>
    <Error Condition="'$(SwapNativeForIL)' == 'true' and '@(CoreCLRILFiles)' == ''" Text="Could not locate CoreCLR IL files." />
  </Target>

  <Target Name="OverrideRuntimeMono"
          DependsOnTargets="ResolveRuntimeFilesFromLocalBuild"
          AfterTargets="AfterResolveReferences"
          Condition="'$(RuntimeFlavor)' == 'Mono'">
    <ItemGroup>
      <!-- copy the host files to make the desktop samples work -->
      <RuntimeFiles Include="@(HostFxrFile)" Condition="Exists('@(HostFxrFile)') and '$(TargetsMobile)' != 'true'"/>
      <RuntimeFiles Include="@(HostPolicyFile)" Condition="Exists('@(HostPolicyFile)') and '$(TargetsMobile)' != 'true'" />
      <ReferenceCopyLocalPaths Include="@(RuntimeFiles)" />
      <!-- Setup runtime pack native. -->
      <ReferenceCopyLocalPaths Include="@(MonoCrossFiles)"
                               DestinationSubDirectory="cross/%(RecursiveDir)" />
      <ReferenceCopyLocalPaths Include="@(MonoIncludeFiles)"
                               DestinationSubDirectory="include/%(RecursiveDir)" />
    </ItemGroup>
  </Target>
</Project>
